//搭建web服务器
const express = require('express');
const app = express();
app.listen(4000, () => {
    console.log('启动了');
});
//解析token,得到req.user
//遇到问题：next(err) 
//解析成功：next()
const jwt = require('express-jwt');
app.use(jwt({
    secret: 'sdf23sd',
    //加密算法
    algorithms: ['HS256']
}).unless({
    path: ['/api/login', '/api/reguser']
}));
//解析查询字符串格式的请求体，得到req.body
app.use(express.urlencoded({
    extended: true
}));
//加载路由模块，注册成中间件
let loginRouter = require('./routers/login');
app.use('/api', loginRouter);
let user = require('./routers/user');
app.use('/my', user);

//错误处理中间件
app.use((err, req, res, next) => {
    if (err.name === 'UnauthorizedError') {
        res.status(401).send({ status: 1, message: '身份认证失败！' });
    };
});